package cn.com.infinitus.uits.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
	
	public static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 

	/**
	 * 以分钟为维度的增幅
	 * @param startTime
	 * @param timeSpace
	 * @return
	 */
	public static String getAddTime4minute(String startTime, int timeSpace) {
		String myString = startTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(myString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.MINUTE, timeSpace);
		myString = sdf.format(cal.getTime());
		return myString;
	}
	
	/**
	 * 以小时为维度的增幅
	 * @param startTime
	 * @param timeSpace
	 * @return
	 */
	public static String getAddTime4hour(String startTime, int timeSpace) {
		String myString = startTime;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(myString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.HOUR, timeSpace);
		myString = sdf.format(cal.getTime());
		return myString;
	}
	
	/**
	 * 以天为维度的增幅
	 * @param startTime
	 * @param timeSpace
	 * @return
	 */
	public static String getAddTime4day(String startTime, int timeSpace) {
		String myString = startTime;
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(dateFormat.parse(myString));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.DATE, timeSpace);
		myString = dateFormat.format(cal.getTime());
		return myString;
	}
	
	/**
	 * 判断时间是否在现在之前
	 * @param datatime
	 * @return
	 */
	public static boolean isBeforeNow4minute(String datatime){
		java.util.Date date=new java.util.Date();  
		String now = dateTimeFormat.format(date);
		
		if(datatime.compareTo(now)<= 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 判断时间是否在现在之前
	 * @param datatime
	 * @return
	 */
	public static boolean isBeforeNow4hour(String datatime){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");  
		java.util.Date date=new java.util.Date();  
		String now = sdf.format(date);
		
		if(datatime.compareTo(now)< 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 判断某个时间是否是今天前
	 * @param datatime
	 * @return
	 */
	public static boolean isBeforeNow4day(String datatime){
		java.util.Date date=new java.util.Date();  
		String now = dateFormat.format(date);
		
		if(datatime.compareTo(now)< 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 得到字符串格式的当前时间
	 * @return
	 */
	public static String getCurrentTime(){
		java.util.Date date=new java.util.Date();  
		return dateTimeFormat.format(date);
	}
	
	
	/**
	 * 得到字Long格式的当前时间
	 * @return
	 */
	public static Long getCurrent(){
		java.util.Date date=new java.util.Date();  	
		return date.getTime();
	}
	
	/**
	 * 得到当前的日期
	 * @return
	 */
	public static String getCurrentDay(){
		java.util.Date date=new java.util.Date();  
		return dateFormat.format(date);
	}
	/**
	 * 取得当前小时
	 * @return
	 */
	public static int getCurrentHour() {
		java.util.Date date=new java.util.Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	/**
	 * 得到年份
	 * @param date
	 * @return
	 */
	public static int getYear(String date){
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(dateFormat.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal.get(Calendar.YEAR);
	}
	
	/**
	 * 当前小时是否小于传入的参数小时
	 * @param hour
	 * @return
	 */
	public static boolean isLateNow4hour(int hour){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(getCurrentTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return hour > cal.get(Calendar.HOUR_OF_DAY);
	}
	
	/**
	 * 得到当前时间是第几月
	 * @param date
	 * @return
	 */
	public static String getMonth(String date){
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(dateFormat.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "" + (cal.get(Calendar.MONTH) +1);
	}
	
	/**
	 * 获取某月的开始一天
	 * @param date
	 * @return
	 */
	public static String getFirstDayOfMonth(String date){
		return getYear(date) + "-" + getMonth(date) + "-1";
	}
	
	/**
     * 获取某月的最后一天
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getLastDayOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,year);
        cal.set(Calendar.MONTH, month-1);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        String lastDayOfMonth = dateFormat.format(cal.getTime());
         
        return lastDayOfMonth;
    }
    
    /**
     * 得到某天是属于一年中第几天
     * @param datetime
     * @return
     */
    public static int getDayofYear(String datetime){
    	Date date = null;
    	try {
    		date = dateFormat.parse(datetime);
    	} catch (ParseException e) {
    		e.printStackTrace();
    	}
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	return  calendar.get(Calendar.DAY_OF_YEAR);
    }
    
    /**
     * 得到昨天的日期
     * @return
     */
    public static String getYesterday(){
    	Calendar   cal   =   Calendar.getInstance();
    	cal.add(Calendar.DATE,   -1);
    	String yesterday = dateFormat.format(cal.getTime());
    	return yesterday;
    }
    /**
     * 得到昨天结束时间戳
     * @return
     */
    public static Date getYesterdayEndTime(){
    	Calendar   calendar   =   Calendar.getInstance();
    	calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);

		// 昨天结束时间
		calendar.add(Calendar.DATE, -1);
		return calendar.getTime();
		
    }
    /**
     * 得到与今天偏离指定天数日期
     * @return
     */
    public static String getCustomDay(int offset){
    	Calendar   cal   =   Calendar.getInstance();
    	cal.add(Calendar.DATE,   offset);
    	String day = dateFormat.format(cal.getTime());
    	return day;
    }
    /*
     * 得到与当前月偏离指定月数的月初
     */
    public static String getMonthStart(int offset){
    	Calendar   cal   =   Calendar.getInstance();
    	cal.add(Calendar.MONTH,   offset);
    	cal.set(Calendar.DAY_OF_MONTH, 1);
    	String day = dateFormat.format(cal.getTime());
    	return day;
    }
    /*
     * 得到与当前月偏离指定月数的月末
     */
    public static String getMonthEnd(int offset){
    	Calendar   cal   =   Calendar.getInstance();
    	cal.add(Calendar.MONTH,   offset);
    	cal.set(Calendar.DAY_OF_MONTH,
    			cal.getActualMaximum(Calendar.DATE));
    	String day = dateFormat.format(cal.getTime());
    	return day;
    }
    
    /**
     * 日期转long
     * @param date
     * @return
     */
    public static long dateToLong(Date date) {
 		return date.getTime();
 	}
    
    /**
     * 日期转long
     * @param date
     * @return
     * @throws ParseException 
     */
    public static long dateToLong(String date) throws ParseException{
		return dateTimeFormat.parse(date).getTime();
	}
    
    /**
     * String类型转date
     * @param strTime
     * @param formatType
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String strTime, String formatType) {
 		SimpleDateFormat formatter = new SimpleDateFormat(formatType);
 		Date date = null;
 		try{
 			date = formatter.parse(strTime);
 		}catch(ParseException e ){
 			e.printStackTrace();
 		}
 		return date;
 	}
    
    /**
     * string转long
     * @param strTime
     * @param formatType
     * @return
     * @throws ParseException
     */
    public static long stringToLong(String strTime, String formatType)
 			throws ParseException {
 		Date date = stringToDate(strTime, formatType); // String类型转成date类型
 		if (date == null) {
 			return 0;
 		} else {
 			long currentTime = dateToLong(date); // date类型转成long类型
 			return currentTime;
 		}
 	}
    
	/**
	 * 时间转字符串
	 * @param date
	 * @param type
	 * @return
	 */
	public static String dateToString(Date date, String type) {  
        String str = null;  
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        if (type.equals("SHORT")) {  
            // 07-1-18  
            format = DateFormat.getDateInstance(DateFormat.SHORT);  
            str = format.format(date);  
        } else if (type.equals("MEDIUM")) {  
            // 2007-1-18  
            format = DateFormat.getDateInstance(DateFormat.MEDIUM);  
            str = format.format(date);  
        } else if (type.equals("FULL")) {  
            // 2007年1月18日 星期四  
            format = DateFormat.getDateInstance(DateFormat.FULL);  
            str = format.format(date);  
        }
        return str;  
    }  
	
	/**
	 * string类型的日期转换为另外一个格式的字符串日期
	 * @param date
	 * @param oldFormat
	 * @param newFormat
	 * @return
	 */
	public static String dateTrans(String date, String oldFormat,
			String newFormat) {
		String result = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
			ParsePosition pos = new ParsePosition(0);
			Date d = sdf.parse(date, pos);
			sdf = new SimpleDateFormat(newFormat);
			result = sdf.format(d);
		} catch (Exception err) {
			System.out.println("Trans Error: " + err.getMessage());
		}
		return result;
	}
	
	/**
	 * 判斷是否為時間格式
	 * @param str
	 * @return
	 */
	public static boolean isValidDate(String str) {
		if(str == null)
			return false;
		boolean convertSuccess = true;
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			// 设置lenient为false.
			// 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			format.parse(str);
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess;
	}


}
